package com.snowfeel.sfmall.sys.controller;

import com.snowfeel.sfmall.core.enums.ResponseCode;
import com.snowfeel.sfmall.core.response.JsonResult;
import com.snowfeel.sfmall.core.vo.PageVo;
import com.snowfeel.sfmall.sys.dto.SysRoleParam;
import com.snowfeel.sfmall.sys.query.SysRoleQuery;
import com.snowfeel.sfmall.sys.service.SysRoleService;
import com.snowfeel.sfmall.sys.vo.SysPermissionVo;
import com.snowfeel.sfmall.sys.vo.SysRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台用户角色管理
 * Created on 2018/9/30.
 */
@RestController
@Api(tags = "SysRoleController", description = "后台用户角色管理")
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    @ApiOperation("添加角色")
    @PostMapping("/create")
    public JsonResult create(@RequestBody SysRoleParam role) {
        int count = roleService.create(role);
        if (count > 0) {
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("修改角色")
    @PostMapping("/update/{id}")
    public JsonResult update(@PathVariable Long id, @RequestBody SysRoleParam role) {
        int count = roleService.update(id, role);
        if (count > 0) {
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("批量删除角色")
    @PostMapping("/delete")
    public JsonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = roleService.delete(ids);
        if (count > 0) {
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("获取相应角色权限")
    @GetMapping("/permission/{roleId}")
    public JsonResult<List<SysPermissionVo>> getPermissionList(@PathVariable Long roleId) {
        List<SysPermissionVo> permissionList = roleService.getPermissionList(roleId);
        return JsonResult.success(permissionList);
    }

    @ApiOperation("修改角色权限")
    @PostMapping("/permission/update")
    public JsonResult updatePermission(@RequestParam Long roleId,
                                         @RequestParam("permissionIds") List<Long> permissionIds) {
        int count = roleService.updatePermission(roleId, permissionIds);
        if (count > 0) {
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("获取所有角色")
    @GetMapping("/page")
    public JsonResult<PageVo<SysRoleVo>> page(@RequestBody SysRoleQuery query) {
        PageVo<SysRoleVo> roleList = roleService.page(query);
        return JsonResult.success(roleList);
    }

}
